#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20  
typedef struct
{
	int data[MAX];
	int top;
}SkNum;//运算数栈 
typedef struct
{
	char data[MAX];
	int top;
}SkChar;//运算符栈 
void InitNum(SkNum* p) {
	p->top = 0;
}
void InitChar(SkChar* p) {
	p->top = 0;
}
void PushNum(SkNum* p, int e) {
	if (p->top == MAX)
		printf("运算数栈满！\n");
	else {
		p->data[p->top] = e;
		p->top++;
	}
}
void PushChar(SkChar* p, char e) {
	if (p->top == MAX)
		printf("运算符栈满！\n");
	else {
		p->data[p->top] = e;
		p->top++;
	}
}
void PopNum(SkNum* p, int* e) {
	if (p->top == 0)
		printf("运算数栈空！\n");
	else {
		p->top--;
		*e = p->data[p->top];
	}
}
void PopChar(SkChar* p, char* e) {
	if (p->top == 0)
		printf("运算符栈空！\n");
	else
	{
		p->top--;
		*e = p->data[p->top];
	}
}
void Fun(SkNum* p, char e) {
	int temp1, temp2;
	PopNum(p, &temp2);
	PopNum(p, &temp1);
	switch (e)
	{
	case '+':PushNum(p, temp1 + temp2); break;
	case '-':PushNum(p, temp1 - temp2); break;
	case '*':PushNum(p, temp1 * temp2); break;
	case '/':PushNum(p, temp1 / temp2); break;

	}
}
int GetNum(SkNum p) {
	return p.data[p.top - 1];
}

void InitNum(SkNum* p);
void PushNum(SkNum* p, int e);
void PopNum(SkNum* p, int* e);
int GetNum(SkNum p);



void InitChar(SkChar* p);
void PushChar(SkChar* p, char e);
void PopChar(SkChar* p, char* e);



void Fun(SkNum* p, char e);



int main()
{
	int i;
	int temp;
	char str[MAX], ch; 
	//-----------
	SkNum n1;
	SkChar c1;
	InitNum(&n1);
	InitChar(&c1);
	//------------
	while (1)
	{
		printf("请输入中缀表达式：");
		gets(str);
		for (i = 0; str[i] != '\0'; i++)//读完整字符串-----字符串结束标志'\0' 
		{
			if (str[i] >= '0' && str[i] <= '9')
			{
				temp = str[i] - '0';


				while (str[i + 1] != '\0')
				{
					if (str[i + 1] >= '0' && str[i + 1] <= '9')
					{
						temp = temp * 10 + str[i + 1] - '0'; 

						i++;
					}
					else
						break; 
				}
				PushNum(&n1, temp);
			}
			else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '(' || str[i] == ')')
			{
				switch (str[i])
				{
				case '+':
					if (c1.data[c1.top - 1] != '+' && c1.data[c1.top - 1] != '-' && c1.data[c1.top - 1] != '*' && c1.data[c1.top - 1] != '/')
					{
						PushChar(&c1, '+');
					}
					else
					{
						while (c1.top > 0 && c1.data[c1.top - 1] != '(') 
						{
							PopChar(&c1, &ch);
							Fun(&n1, ch);

						}
						PushChar(&c1, '+');
					}
					; break;
				case '-':
					if (c1.data[c1.top - 1] != '+' && c1.data[c1.top - 1] != '-' && c1.data[c1.top - 1] != '*' && c1.data[c1.top - 1] != '/'){
						PushChar(&c1, '-');
					}
					else{
						while (c1.top > 0 && c1.data[c1.top - 1] != '('){
							PopChar(&c1, &ch);
							Fun(&n1, ch);

						}
						PushChar(&c1, '-');
					}
					; break;
				case '*':
					if (c1.data[c1.top - 1] != '*' && c1.data[c1.top - 1] != '/'){
						PushChar(&c1, '*');
					}
					else{
						while (c1.top > 0 && c1.data[c1.top - 1] != '(')
						{
							PopChar(&c1, &ch);
							Fun(&n1, ch);

						}
						PushChar(&c1, '*');
					}
					; break;
				case '/':
					if (c1.data[c1.top - 1] != '*' && c1.data[c1.top - 1] != '/'){
						PushChar(&c1, '/');
					}
					else{
						while (c1.top > 0 && c1.data[c1.top - 1] != '(') 
						{
							PopChar(&c1, &ch);
							Fun(&n1, ch);

						}
						PushChar(&c1, '/');
					}
					; break;
				case '(':

					PushChar(&c1, '(');

					; break;
				case ')': 
					while (c1.data[c1.top - 1] != '('){
						PopChar(&c1, &ch);
						Fun(&n1, ch);
					}
					PopChar(&c1, &ch);
					; break;
				}

			}
		}
		while (c1.top > 0) {
			PopChar(&c1, &ch);
			Fun(&n1, ch);
		}
		printf("\t\t%s=%.2d", str, GetNum(n1));
		printf("\n");
		system("pause");
	}

}







